package com.xcdh.domain;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.ognl.Ognl;
import org.apache.ibatis.ognl.OgnlException;

@Data
@Slf4j
public class RequestPage<T> extends Page<T>  {

    private QueryWrapper<T> query ;
    private T model ;
    public RequestPage() {
        super(1,20);
    }

    public void setModel(T model){
        this.model = model ;
        query = new QueryWrapper<>(model) ;
    }

    public static boolean notNull(Object data){
        return data != null  ;
    }
    public  Object getValue(String property){
        if(model == null){
            return null ;
        }
        try {
            return Ognl.getValue(property , model) ;
        } catch (OgnlException e) {
            e.printStackTrace();
            return null ;
        }
    }
    public Object has(String property){
        Object value = getValue(property);
        if(value == null){
            return false ;
        }
        if(value instanceof String){
            return !isBlank(value.toString()) ;
        }
        return true ;
    }

    public static String trim(String val){
        return val.replaceAll("\\s","");
    }

    public static boolean isBlank(String val){
        return val == null || trim(val).length() == 0 ;
    }
}
